package cart

import (
	"context"
	"enjoy-shop/formdata"
	"errors"
	"gitee.com/enjoy_5/cart-rpc/client"
	proto "gitee.com/enjoy_5/cart-rpc/proto"
	client2 "gitee.com/enjoy_5/goods-rpc/client"
	"gorm.io/gorm"
)

// 添加商品到购物车
func CreateGoodsCart(cart formdata.Cart) (*proto.EnjoyCart, error) {
	//通过token获取userid
	//通过skuId获取库存
	ds, err := client2.GetSkuInfoByIDs(context.Background(), int64(cart.SkuId))
	if err != nil {
		return nil, err
	}
	if ds.Stock < int64(cart.Quantity) {
		return nil, err
	}
	//检查该用户是否已添加过该商品(通过user_id和goods_id查询购物车详情,如果为（空 或 查询到的sku_id和添加的sku_id不匹配)添加,否则修改购物车商品数量)
	info, err := client.GetCartInfo(context.Background(), cart.UserId, cart.GoodsId)
	if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
		//添加商品到购物车
		goods, err := client.CreateCartGoods(context.Background(), &proto.EnjoyCart{
			UserId:   cart.UserId,
			GoodsId:  cart.GoodsId,
			Quantity: cart.Quantity,
			SkuId:    cart.SkuId,
		})
		if err != nil {
			return &proto.EnjoyCart{}, err
		}
		return goods, nil
	}
	if info.SkuId == cart.SkuId {
		goods, err := client.UpdateCartGoods(context.Background(), cart.UserId, cart.GoodsId, cart.Quantity+1)
		if err != nil {
			return nil, err
		}
		return goods, nil
	}
	return nil, nil
}

// 删除购物车商品
func DeleteGoodsCart(cart formdata.DeleteCart) (*proto.EnjoyCart, error) {
	//通过token获取用户id(通过token获取用户的id)

	//删除购物车商品
	goods, err := client.DeleteCartGoods(context.Background(), cart.UserId, cart.GoodsId)
	if err != nil {
		return &proto.EnjoyCart{}, err
	}
	return goods, nil
}

// 修改购物车商品
func UpdateGoodsCart(cart formdata.Cart) (*proto.EnjoyCart, error) {
	//通过token获取用户id(通过token获取用户的id)
	//通过skuId获取到库存和状态(验证库存是否充足,状态是否正常)
	ds, err := client2.GetSkuInfoByIDs(context.Background(), int64(cart.SkuId))
	if err != nil {
		return nil, err
	}
	if ds.Stock < int64(cart.Quantity) {
		return nil, err
	}
	//修改商品购买数量
	goods, err := client.UpdateCartGoods(context.Background(), cart.UserId, cart.GoodsId, cart.Quantity)
	if err != nil {
		return &proto.EnjoyCart{}, err
	}
	return goods, nil
}

// 查询购物车列表
func GetCartListByUserId(cart formdata.Cart) ([]*formdata.CartList, error) {
	//通过token获取用户id(通过token获取用户的id)
	//查询购物车列表
	list, err := client.GetCartListByUserId(context.Background(), cart.UserId)
	if err != nil {
		return nil, err
	}
	var goods []*formdata.CartList
	for _, v := range list {
		s, err := client2.GetSkuInfoByIDs(context.Background(), int64(v.SkuId))
		if err != nil {
			return nil, err
		}
		if s.Stock < int64(v.Quantity) {
			return nil, err
		}
		info, err := client2.GetGoodsInfo(context.Background(), int64(v.GoodsId))
		if err != nil {
			return nil, err
		}

		goods = append(goods, &formdata.CartList{
			GoodsId:     int64(v.GoodsId),
			Stock:       s.Stock,
			Price:       s.Price,
			ProValueId1: s.ProValueId1,
			ProValueId2: s.ProValueId2,
			ProValueId3: s.ProValueId3,
			VipPrice:    s.VipPrice,
			Title:       info.Title,
			Img:         info.Img,
			Description: info.Description,
			Quantity:    v.Quantity,
			SkuId:       int64(v.SkuId),
		})
	}
	return goods, nil
}
